package com.buss.job;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.jweb.core.bean.PageMode;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.buss.common.service.SysServiceI;
import com.buss.job.entity.HxJobEntity;
import com.buss.job.trans.form.HxJobSearchForm;
import com.buss.job.trans.vo.HxJobContentVO;
import com.buss.job.trans.vo.HxJobVO;


@Controller
@RequestMapping("/job")
public class JobController {
	
	@Autowired
	private SysServiceI sysService;
	
	@RequestMapping({ "/list.do" })
	@ResponseBody
	public ReplyDataMode serverList(HttpServletRequest req){
		ReplyDataMode j = new ReplyDataMode();

		List<Map> mapList = new ArrayList<Map>();
		Map map = null;
		
		String sql = "SELECT job_name,job_description from hx_job where status=1 and ispush=1 ";
		List<String[]> dataList = sysService.findListbySql(sql);
		
		for (Object obj : dataList) {
			Object[] u = (Object[]) obj;
			map=new HashMap();			
			if(u[0]!=null){
				map.put("name", u[0].toString());
			}
			if(u[1]!=null){
				map.put("text", u[1].toString());
			}
			mapList.add(map);
		}
		
		Map resultMap = new HashMap();
		resultMap.put("list", mapList);
		Object obj = JSON.toJSON(resultMap);
		
		j.setData(obj);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 分页查询
	 * @param searchForm
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping({ "/listPage.do" })
	@ResponseBody
	public ReplyDataMode listPage(HxJobSearchForm searchForm, HttpServletRequest request)
			throws ServletException, IOException {
		ReplyDataMode j = new ReplyDataMode();
		
		// 手动加条件
		searchForm.setStatus("1"); // 1正常
		searchForm.setIspush("1"); // 1发布
		CriteriaQuery cq = searchForm.getCriteriaQuery();
		
		Map<String,Object> orderMap = new HashMap<String,Object>();
		orderMap.put("jobPushtime", "desc");
		cq.setOrder(orderMap);
		int pageNo = Integer.parseInt(searchForm.getPageNo());
		int pageSize = Integer.parseInt(searchForm.getPageSize());
//		cq.setPageSize(pageSize-1);
		// 分页
		PageMode page = this.sysService.list4page(cq, pageNo, pageSize-1);
		List<HxJobEntity> entityList = page.getResult();
		List<HxJobVO> voList = new ArrayList<HxJobVO>();
		HxJobVO vo = null;
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
		for(HxJobEntity e : entityList){
			vo = new HxJobVO();
			vo.copyEntity(e);
			if(e.getJobPushtime()!=null){
				vo.setJobPushtime(format2.format(e.getJobPushtime()));
			}
			voList.add(vo);
		}
		
		page.setResult(voList);
		
		j.setData(page);
		j.setSuccess(true);
		
		return j;
	}
	
	
	/**
	 * id查询
	 * @param searchForm
	 * @param request
	 * @return
	 */
	@RequestMapping({ "/jobByIdForContent.do" })
	@ResponseBody
	public ReplyDataMode jobByIdForContent(HttpServletRequest request)
			throws ServletException, IOException {
		ReplyDataMode j = new ReplyDataMode();
		List<HxJobEntity> entityList = null;
		
		String id = request.getParameter("id");
		if(id!=null&&id!=""){			
			entityList = sysService.findByProperty(HxJobEntity.class, "id", Integer.parseInt(id));
		}
		
		List<HxJobContentVO> voList = new ArrayList<HxJobContentVO>();
		HxJobContentVO vo = null;
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
		for(HxJobEntity e : entityList){
			vo = new HxJobContentVO();
			vo.copyEntity(e);
			if(e.getJobPushtime()!=null){
				vo.setJobPushtime(format2.format(e.getJobPushtime()));
			}
			voList.add(vo);
		}
		
		j.setData(voList);
		j.setSuccess(true);
		
		return j;
	}
	
	
}
